<?php

/**
 * DoctrineTemplateSluggableExt
 *
 * Easily create a slug for each record based on a specified set of fields
 *
 * @package     flexiwik
 * @subpackage  template
 */
class DoctrineTemplateSluggableExt extends Doctrine_Template_Sluggable
{
  public function __construct(array $options = array())
  {
    $options = array_merge(array('builder' => array('kcInflector', 'urlize')), $options);
    parent::__construct($options);
  }
  
  public function setTableDefinition()
  {
    $name = $this->_options['name'];
    if ($this->_options['alias'])
    {
      $name .= ' as ' . $this->_options['alias'];
    }
    $this->hasColumn($name, $this->_options['type'], $this->_options['length'], $this->_options['options']);
    
    if ($this->_options['unique'] == true && $this->_options['uniqueIndex'] == true && ! empty($this->_options['fields']))
    {
      $indexFields = array($this->_options['name']);
      $indexFields = array_merge($indexFields, $this->_options['uniqueBy']);
      $this->index($this->_options['indexName'], array('fields' => $indexFields, 'type' => 'unique'));
    }

    $this->addListener(new DoctrineTemplateListenerSluggableExt($this->_options));
  }
}
